import java.util.ArrayList;
import java.util.List;

//https://leetcode.cn/problems/letter-combinations-of-a-phone-number/
class Solution {
    List<String> ret;
    StringBuffer path;
    String[] reflect = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    public List<String> letterCombinations(String digits) {
        ret = new ArrayList<>();
        if(digits.equals("")) return ret;
        path = new StringBuffer();
        dfs(digits, 0);
        return ret;
    }
    public void dfs(String digits, int pos) {
        if(path.toString().length() == digits.length()) {
            ret.add(path.toString());
            return;
        }
        int index = digits.charAt(pos) - '0';
        for(int i = 0; i < reflect[index].length(); i++) {
            path.append(reflect[index].charAt(i));
            dfs(digits, pos + 1);
            //回溯 -> 恢复现场
            path.deleteCharAt(path.toString().length()-1);
        }
    }
}